#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QWheelEvent>
#include <QPoint>
#include <QPaintEvent>
#include <QOpenGLFunctions>
// Add these at the top with other includes
#include <QVector3D>
#include <QQuaternion>

class GLWidget : public QOpenGLWidget, protected QOpenGLFunctions {
    Q_OBJECT
public:
    explicit GLWidget(QWidget *parent = nullptr);
    ~GLWidget();

    // 添加枚举类型声明
    enum NodeType {
        DEVICE_PHONE,
        DEVICE_ROUTER,
        DEVICE_COMPUTER,
        CUSTOM_TEXTURE
    };
    
    struct Node {
        float relativeX, relativeY; // 相对于平面中心的坐标
        float z;          // 绝对高度
        bool selected;
        NodeType type;
    };
    
protected:
    void initializeGL() override;
    void resizeGL(int w, int h) override;
    void paintGL() override;
//    void paintEvent(QPaintEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
//    void keyPressEvent(QKeyEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;

// 在GLWidget类中添加
public:
    // 平面创建接口
    void createPlane(float zLevel, float size, const QColor& gridColor = Qt::gray);
    
    // 节点管理接口
    void addNodeToPlane(int planeIndex, float x, float y, NodeType type);
    void generateRandomConnections(int planeIndex, int minConnections, int maxConnections);
    void generateInterPlaneConnections(int sourcePlane, int targetPlane, int connectionCount);
    void arrangeNodesInCircle(int planeIndex);
    void arrangeNodesInGrid(int planeIndex, int cols);
    void arrangeNodesRandomly(int planeIndex, float margin = 0.8f);
    void setupLighting();
    void drawEnhancedCube(float x, float y, float z, float width, float height, float depth, float r, float g, float b, float alpha);
    void drawEnhancedNode(const Node& node, float planeHeight);
    int getNodeAtPosition(int x, int y);
    void selectNode(int nodeIndex);
    void clearSelection();
    void optimizeRendering();
    void createDisplayLists();
    void deleteDisplayLists();
    void drawConnection(const Node& node1, const Node& node2, int plane1Idx, int plane2Idx);

// 在GLWidget类中添加连接存储结构
private:
    // Add these to existing private members
    bool m_viewDirty = true;  // Track view matrix changes
    
    // Camera control variables
    QVector3D m_rotationAxis;
    QQuaternion m_rotation;
    QVector3D m_translation;
    
    // Enhanced camera system
    float cameraDistance;
    float rotationX;
    float rotationY;
    float targetRotationX;  // For smooth rotation
    float targetRotationY;  // For smooth rotation
    float targetCameraDistance;  // For smooth zooming
    float minCameraDistance;
    float maxCameraDistance;
    QPoint lastMousePosition;
    bool isDragging;
    
    // Connection and plane structures
    struct Connection {
        int node1;
        int node2;
        int plane1;  // Source plane index
        int plane2;  // Target plane index
    };
    std::vector<Connection> connections;
    
    struct Plane {
        float zLevel;
        float size;
        QColor gridColor;
        std::vector<int> nodeIndices;
    };
    
    std::vector<Plane> planes;
    
    // Rendering helpers
    GLuint loadTexture(const QString &filePath);
    GLuint textures[3];
    
    // 性能优化相关
    GLuint planeDisplayList;
    GLuint nodeDisplayList;
    bool displayListsCreated;
    void updateCameraSmooth();
};

#endif // GLWIDGET_H
